//
//  DCMineViewController.m
//  Demo
//
//  Created by Kyle on 2022/12/31.
//

#import "DCMineViewController.h"
#import "DCMineVCTableCell.h"
#import "DCSettingViewController.h"
#import "DCMineBankCardController.h"
#import "DCMineServicesController.h"
#import "DCWebViewController.h"

@interface DCMineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIImageView *mineHeader;

@property (nonatomic, strong) UILabel *useLabel;

@property (nonatomic, strong) UILabel *unUseLabel;

@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation DCMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getDataFromNet];
    // Do any additional setup after loading the view.
}

- (void)configureAlloc {
    [super configureAlloc];
    self.dataSource = @[@{@"titleName":@"Contact us", @"imageName":@"icon_Contact"},
                        @{@"titleName":@"Bank card", @"imageName":@"icon_Bank_Card"},
                        @{@"titleName":@"Settings", @"imageName":@"icon_mine_settings"},
                        @{@"titleName":@"Privacy Agreement", @"imageName":@"icon_Privacy_Agreement"},].mutableCopy;
}

- (void)configureUI {
    [super configureUI];
    self.navgitBar.hidden = YES;
    self.navgitBar.backBtn.hidden = YES;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.tableView registerClass:DCMineVCTableCell.class forCellReuseIdentifier:NSStringFromClass(DCMineVCTableCell.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = self.view.backgroundColor ;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 336+KSafeAreaInset.top + 20)];
    headerView.backgroundColor = [UIColor colorWithString:@"#F0FFCE"];
    self.tableView.tableHeaderView = headerView;
    
    UIImageView *headBG = [[UIImageView alloc] initWithFrame:headerView.bounds];
    headBG.image = [UIImage imageNamed:@"mine_header_bg"];
    [headerView addSubview:headBG];
    [headBG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(KSafeAreaInset.top);
    }];
    
    _mineHeader = [[UIImageView alloc] init];
    _mineHeader.layer.cornerRadius = 54/2.0;
    _mineHeader.layer.masksToBounds = YES;
    _mineHeader.image = [UIImage imageNamed:@"icon_mine_logo"];
    [headerView addSubview:_mineHeader];
    [_mineHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(49+KSafeAreaInset.top);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.height.mas_equalTo(54);
    }];
    
    UILabel *nameLabel = [UILabel new];
    nameLabel.text = @"HI!******";
    nameLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    nameLabel.font = DCFont_PF_Medium(20);
    [headerView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(_mineHeader.mas_bottom).mas_offset(20);
    }];
  
    UIView *colorView = [UIView new];
    colorView.backgroundColor = [UIColor colorWithString:@"#1C221B"];
    colorView.layer.cornerRadius = 4;
    colorView.layer.masksToBounds = YES;
    [headerView addSubview:colorView];
    [colorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.right.mas_equalTo(-18);
        make.top.mas_equalTo(nameLabel.mas_bottom).mas_offset(20);
        make.height.mas_equalTo(100);
    }];
    
    UIView *centerLine = [UIView new];
    centerLine.backgroundColor = [UIColor colorWithString:@"#E1FE97"];
    [colorView addSubview:centerLine];
    [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(colorView.mas_centerX);
        make.top.mas_equalTo(33);
        make.bottom.mas_equalTo(-33);
        make.width.mas_equalTo(1);
    }];
    //Unpaid amount
    
    UIView *leftCenter  = [UIView new];
    leftCenter.backgroundColor = [UIColor clearColor];
    [colorView addSubview:leftCenter];
    [leftCenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(centerLine.mas_left);
    }];
    
    UIView *rightCenter  = [UIView new];
    rightCenter.backgroundColor = [UIColor clearColor];
    [colorView addSubview:rightCenter];
    [rightCenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(centerLine.mas_right);
    }];
    
    UILabel *unUseLabel = [UILabel new];
    _unUseLabel = unUseLabel;
    unUseLabel.text = @"--";
    unUseLabel.textColor = [UIColor colorWithString:@"#E1FE97"];
    unUseLabel.font = DCFont_PF_Medium(18);
    [leftCenter addSubview:unUseLabel];
    [unUseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(leftCenter.mas_centerY).mas_offset(-6);
        make.centerX.mas_equalTo(leftCenter);
    }];
    
    UILabel *unUseDes = [UILabel new];
    unUseDes.text = @"Available credit";
    unUseDes.textColor = [UIColor colorWithString:@"#E1FE97"];
    unUseDes.font = DCFont_PF_Regular(12);
    [leftCenter addSubview:unUseDes];
    [unUseDes mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(leftCenter.mas_centerY).mas_offset(6);
        make.centerX.mas_equalTo(leftCenter);
    }];
        
    UILabel *useLabel = [UILabel new];
    _useLabel = useLabel;
    useLabel.text = @"--";
    useLabel.textColor = [UIColor colorWithString:@"#E1FE97"];
    useLabel.font = DCFont_PF_Medium(18);
    [rightCenter addSubview:useLabel];
    [useLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(rightCenter.mas_centerY).mas_offset(-6);
        make.centerX.mas_equalTo(rightCenter);
    }];
    
    UILabel *useDes = [UILabel new];
    useDes.text = @"Unpaid amount";
    useDes.textColor = [UIColor colorWithString:@"#E1FE97"];
    useDes.font = DCFont_PF_Regular(12);
    [rightCenter addSubview:useDes];
    [useDes mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(rightCenter.mas_centerY).mas_offset(6);
        make.centerX.mas_equalTo(rightCenter);
    }];
    
    UIView *radiusView = [UIView new];
    radiusView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    radiusView.layer.cornerRadius = 30;
    radiusView.layer.masksToBounds = YES;
    [headerView addSubview:radiusView];
    [radiusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.left.mas_equalTo(0);
        make.height.mas_equalTo(60);
    }];
    
    UIView *view2 = [UIView new];
    view2.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [headerView addSubview: view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(30);
    }];
    
//    UIImageView *lineView = [[UIImageView alloc] init];
//    lineView.backgroundColor = [UIColor colorWithString:@"#C7C7C7"];
//    [view2 addSubview:lineView];
//    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.bottom.mas_equalTo(0);
//        make.height.mas_equalTo(1);
//    }];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)getDataFromNet {
    [DCHttpRequest DCBaseUserInfoCompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        if (responseObject.code == 200) {
            DCUserInfo *userInfo = [[CacheTools sharedCacheTools] getObjForKey:SMARTUSERINFO];
            NSMutableDictionary *mArr = userInfo.mj_keyValues;
            [mArr addEntriesFromDictionary:responseObject.data];
            userInfo = [DCUserInfo mj_objectWithKeyValues:mArr];
            [[CacheTools sharedCacheTools] setObjectForKey:userInfo key:SMARTUSERINFO];
            [self upDataHeaderData];
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}

- (void)upDataHeaderData {
    DCUserInfo *userInfo = [[CacheTools sharedCacheTools] getObjForKey:SMARTUSERINFO];
//    _mineHeader
    _useLabel.text = [NSString stringWithFormat:@"%@",userInfo.unused];
    _useLabel.text = [NSString stringWithFormat:@"%@",userInfo.repaymentTotalAmount];
}

#pragma mark -table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DCMineVCTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCMineVCTableCell.class)];
    NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.row];
    [cell setDataWithDic:dic lineHidden:indexPath.row == self.dataSource.count-1];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 68;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    switch (indexPath.row) {
        case 0:
        {
            DCMineServicesController *vc = [[DCMineServicesController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            DCMineBankCardController *vc = [DCMineBankCardController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            DCSettingViewController *settingVC = [DCSettingViewController new];
            [self.navigationController pushViewController:settingVC animated:YES];
        }
            break;
        case 3:
        {
            DCWebModel *webInfo = [DCWebModel creatPrivacyAgreement];
            DCWebViewController *webVC = [[DCWebViewController alloc] initWithModel:webInfo];
            [self.navigationController pushViewController:webVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}
@end
